home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / resize2a / resizer.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-10-14  |  8.8 KB  |  262 lines

  1. VERSION 5.00
  2. Begin VB.UserControl Resizer 
  3.    ClientHeight    =   480
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   495
  7.    InvisibleAtRuntime=   -1  'True
  8.    ScaleHeight     =   480
  9.    ScaleWidth      =   495
  10.    ToolboxBitmap   =   "Resizer.ctx":0000
  11.    Begin VB.Image Image1 
  12.       Height          =   480
  13.       Left            =   0
  14.       Picture         =   "Resizer.ctx":0182
  15.       Top             =   0
  16.       Width           =   480
  17.    End
  18. Attribute VB_Name = "Resizer"
  19. Attribute VB_GlobalNameSpace = False
  20. Attribute VB_Creatable = True
  21. Attribute VB_PredeclaredId = False
  22. Attribute VB_Exposed = True
  23. Option Explicit
  24. ' if True, also fonts are resized '
  25. Public ResizeFont As Boolean
  26. ' if True, form's height/width ratio is preserved '
  27. Public KeepRatio As Boolean
  28. Private Type TControlInfo
  29.        
  30.        ctrl As Control
  31.        Left As Single
  32.        Top As Single
  33.        Width As Single
  34.        Height As Single
  35.        FontSize As Single
  36.        
  37. End Type
  38. Private Type TAllowChanges
  39.        AllowChangeTop As Boolean
  40.        AllowChangeLeft As Boolean
  41.        AllowChangeWidth As Boolean
  42.        AllowChangeHeight As Boolean
  43.         
  44. End Type
  45. ' this array holds the original position  '
  46. ' and size of all controls on parent form '
  47. Dim Controls() As TControlInfo
  48. ' a reference to the parent form '
  49. Private WithEvents ParentForm As Form
  50. Attribute ParentForm.VB_VarHelpID = -1
  51. ' parent form's size at load time '
  52. Private ParentWidth As Single
  53. Private ParentHeight As Single
  54. ' ratio of original height/width '
  55. Private HeightWidthRatio As Single
  56. Private Function CheckForChanges(ByVal TagToUse As String) As TAllowChanges
  57.   Dim ChangesToAllow As TAllowChanges
  58.   ChangesToAllow.AllowChangeTop = True
  59.   ChangesToAllow.AllowChangeLeft = True
  60.   ChangesToAllow.AllowChangeWidth = True
  61.   ChangesToAllow.AllowChangeHeight = True
  62.   If TagToUse <> "" Then
  63.     If UCase(Left(TagToUse, 9)) = "MSIRESIZE" Then
  64.       
  65.       ChangesToAllow.AllowChangeTop = False
  66.       ChangesToAllow.AllowChangeLeft = False
  67.       ChangesToAllow.AllowChangeWidth = False
  68.       ChangesToAllow.AllowChangeHeight = False
  69.       If Mid(TagToUse, 10, 1) = "Y" Then
  70.       
  71.         ChangesToAllow.AllowChangeLeft = True
  72.         
  73.       End If
  74.       
  75.       If Mid(TagToUse, 11, 1) = "Y" Then
  76.       
  77.         ChangesToAllow.AllowChangeTop = True
  78.         
  79.       End If
  80.       
  81.       If Mid(TagToUse, 12, 1) = "Y" Then
  82.       
  83.         ChangesToAllow.AllowChangeWidth = True
  84.         
  85.       End If
  86.       
  87.       If Mid(TagToUse, 13, 1) = "Y" Then
  88.       
  89.         ChangesToAllow.AllowChangeHeight = True
  90.         
  91.       End If
  92.       
  93.     End If
  94.   End If
  95.   CheckForChanges = ChangesToAllow
  96. End Function
  97. Private Sub ParentForm_Load()
  98.   ' the ParentWidth variable works as a flag '
  99.   ParentWidth = 0
  100.   ' save original ratio '
  101.   HeightWidthRatio = ParentForm.Height / ParentForm.Width
  102. End Sub
  103. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  104.   ResizeFont = PropBag.ReadProperty("ResizeFont", False)
  105.   KeepRatio = PropBag.ReadProperty("KeepRatio", False)
  106.   If Ambient.UserMode = False Then
  107.     Exit Sub
  108.   End If
  109.   ' store a reference to the parent form and start receiving events '
  110.   Set ParentForm = Parent
  111. End Sub
  112. Private Sub UserControl_Resize()
  113.   ' refuse to resize '
  114.   Image1.Move 0, 0
  115.   UserControl.Width = Image1.Width
  116.   UserControl.Height = Image1.Height
  117. End Sub
  118. ''''''''''''''''''''''''''''''''''''''''''''
  119. ' trap the parent form's Resize event      '
  120. ' this include the very first resize event '
  121. ' that occurs soon after form's load       '
  122. ''''''''''''''''''''''''''''''''''''''''''''
  123. Private Sub ParentForm_Resize()
  124.   If ParentWidth = 0 Then
  125.     Rebuild
  126.   Else
  127.     Refresh
  128.   End If
  129. End Sub
  130. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  131. ' save size and position of all controls on parent form                  '
  132. ' you should manually invoke this method each time you add a new control '
  133. ' to the form (through Load method of a control array)                   '
  134. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  135. Sub Rebuild()
  136.   ' rebuild the internal table
  137.   Dim i As Integer
  138.   Dim ctrl As Control
  139. '  Dim Changes As TAllowChanges
  140.   ' this is necessary for controls that don't support
  141.   ' all properties (e.g. Timer controls)
  142.   On Error Resume Next
  143.   If Ambient.UserMode = False Then
  144.     Exit Sub
  145.   End If
  146.   ' save a reference to the parent form, and its initial size
  147.   Set ParentForm = UserControl.Parent
  148.   ParentWidth = ParentForm.ScaleWidth
  149.   ParentHeight = ParentForm.ScaleHeight
  150.   ' read the position of all controls on the parent form
  151.   ReDim Controls(ParentForm.Controls.Count - 1) As TControlInfo
  152.   For i = 0 To ParentForm.Controls.Count - 1
  153.      
  154.      Set ctrl = ParentForm.Controls(i)
  155.         
  156. '     Changes = CheckForChanges(ctrl)
  157.      
  158.      With Controls(i)
  159.                  Set .ctrl = ctrl
  160.                      
  161. '                     If Changes.AllowChangeLeft = True Then
  162.                        .Left = ctrl.Left
  163. '                     End If
  164. '                     If Changes.AllowChangeTop = True Then
  165.                        .Top = ctrl.Top
  166. '                     End If
  167. '                     If Changes.AllowChangeTop = True Then
  168.                        .Width = ctrl.Width
  169. '                     End If
  170. '                     If Changes.AllowChangeTop = True Then
  171.                        .Height = ctrl.Height
  172. '                     End If
  173.                      .FontSize = ctrl.Font.Size
  174.      End With
  175.         
  176.   Next
  177. End Sub
  178. '''''''''''''''''''''''''''''''''''''''''''''''''''''''
  179. ' update size and position of controls on parent form '
  180. '''''''''''''''''''''''''''''''''''''''''''''''''''''''
  181. Sub Refresh()
  182.   Dim i As Integer
  183.   Dim ctrl As Control
  184.   Dim minFactor As Single
  185.   Dim widthFactor As Single
  186.   Dim heightFactor As Single
  187.   Dim Changes As TAllowChanges
  188.   ' inhibits recursive calls if KeepRatio = True '
  189.   Static executing As Boolean
  190.   If executing Then
  191.     Exit Sub
  192.   End If
  193.   If Ambient.UserMode = False Then
  194.     Exit Sub
  195.   End If
  196.   If KeepRatio Then
  197.     executing = True
  198.     ' we must keep original ratio '
  199.     ParentForm.Height = HeightWidthRatio * ParentForm.Width
  200.     executing = False
  201.   End If
  202.   ' this is necessary for controls that don't support '
  203.   ' all properties (e.g. Timer controls)              '
  204.   On Error Resume Next
  205.   widthFactor = ParentForm.ScaleWidth / ParentWidth
  206.   heightFactor = ParentForm.ScaleHeight / ParentHeight
  207.   ' take the lesser of the two '
  208.   If widthFactor < heightFactor Then
  209.     minFactor = widthFactor
  210.   Else
  211.     minFactor = heightFactor
  212.   End If
  213.   ' this is a regular resize '
  214.   For i = 0 To UBound(Controls)
  215.         
  216.      Changes = CheckForChanges(Controls(i).ctrl.Tag)
  217.      
  218.      With Controls(i)
  219.             
  220.                      ' the change of font must occur *before* the resizing '
  221.                      ' to account for companion scrollbar of listbox '
  222.                      ' and other similar controls '
  223.                      If ResizeFont Then
  224.                        
  225.                        .ctrl.Font.Size = .FontSize * minFactor
  226.                      
  227.                      End If
  228.                      
  229.                      ' move and resize the controls - we can't use a Move '
  230.                      ' method because some controls do not support the change '
  231.                      ' of all the four properties (e.g. Height with comboboxes) '
  232.                      If Changes.AllowChangeLeft = True Then
  233.                        
  234.                        .ctrl.Left = .Left * widthFactor
  235.                      
  236.                      End If
  237.                      
  238.                      If Changes.AllowChangeTop = True Then
  239.                        
  240.                        .ctrl.Top = .Top * heightFactor
  241.                      
  242.                      End If
  243.                      
  244.                      If Changes.AllowChangeWidth = True Then
  245.                        
  246.                        .ctrl.Width = .Width * widthFactor
  247.                      
  248.                      End If
  249.                      
  250.                      If Changes.AllowChangeHeight = True Then
  251.                        
  252.                        .ctrl.Height = .Height * heightFactor
  253.                      
  254.                      End If
  255.      End With
  256.   Next
  257. End Sub
  258. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  259.   Call PropBag.WriteProperty("ResizeFont", ResizeFont, False)
  260.   Call PropBag.WriteProperty("KeepRatio", KeepRatio, False)
  261. End Sub
  262.